package com.jorado.ognl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Map;
import java.util.Random;

/**
 * @Auther: len.zhang
 * @Date: 2019/8/14 11:09
 * @Description: 自定义函数，F.
 */
public class Func {

    private static final Logger LOGGER = LoggerFactory.getLogger(Func.class);

    /**
     * 测试函数
     *
     * @param what
     */
    public static void say(Object what) {
        System.out.println(what);
    }

    /**
     * 获取随机对象
     *
     * @return
     */
    public static Random random() {
        return new Random();
    }

    /**
     * 激活函数
     *
     * @param value
     * @return
     */
    public static double sigmoid(double value) {
        return 1.0 / (1 + Math.exp(-value));
    }

    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    public static boolean isEmpty(Collection<?> collection) {
        return (collection == null || collection.isEmpty());
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return (map == null || map.isEmpty());
    }

    public static boolean isNotEmpty(Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static boolean isNullOrEmpty(Object o) {
        if (null == o) {
            return true;
        }
        String val = o.toString();
        if (isBlank(val)) {
            return true;
        }
        if ("null".equalsIgnoreCase(val)) {
            return true;
        }
        return false;
    }

    public static boolean isNotNullOrEmpty(Object o) {
        return !isNullOrEmpty(o);
    }

    public static int sum(Collection<Integer> values) {
        int value = 0;
        if (isEmpty(values)) {
            return value;
        }
        for (Integer v : values) {
            if (v != null) {
                value += v;
            }
        }
        return value;
    }

    public static int sum(Map<Object, Integer> datas) {
        int value = 0;
        if (isEmpty(datas)) {
            return value;
        }
        for (Map.Entry<Object, Integer> entry : datas.entrySet()) {
            if (entry.getValue() != null) {
                value += entry.getValue();
            }
        }
        return value;
    }
}
